﻿function [ rbf ] = AdaBoost( X,Y,D,T )
%	Adaboost-Adaptive Boosting
%   Input:
%       X - train set.
%       Y - train labels.
%       D - distribution function over X times X, it the form of 2D matrix.
%       T - number of iteration of the boosting.
%   Output:
%       rbf - Ranking Function.

rbf = RankBoostFunc(T);
% w - the current distribution in any iteration, initilize to D
w = D;
for t=1:T
    tic;
    fprintf('RankBoost: creating the function, iteration %d out of %d\n',t,T);
    WL = getBestWeakLearner(X,Y,w);
    rbf.addWeakLearner(WL,t);
    rbf.addAlpha(WL.alpha,t);
    alpha=WL.alpha;
    
    %update the distribution
    %eval the weak learnler on the set of X and Y
    h=WL.eval(X);
    [hlen, ~] = size(h);
    w = w./sum(w(:));
    % tmph = (repmat(h,1,hlen) - repmat(h',hlen,1));
    %w=w.*exp(tmph.*alpha);
    [rows, cols] = size(w);
    sumw = 0;
    for r=1:rows
        for c=1:cols
            w(r,c) = w(r,c)*exp((h(r)-h(c))*alpha);
            sumw = sumw + w(r,c);
        end
    end
    %normalize w
    %w = w./sum(w(:));
    w = w./sumw;
    toc;
end
end